package model

import "gorm.io/gorm"

type Message struct {
	gorm.Model
	UserId   int    `gorm:"type:int" json:"userId"`
	FriendId int    `gorm:"type:int" json:"friendId"`
	Content  string `gorm:"type:varchar(100)" json:"content"`
}

func (m *Message) AddMessage(db *gorm.DB) error {
	return db.Create(&m).Error
}

type MessageList struct {
	Content string
}

func (m *Message) GetMessage(db *gorm.DB, userId, friendId int) (list []MessageList, err error) {
	err = db.Model(&m).Select("content").
		Where("user_id = ? and friend_id = ? or user_id = ? and friend_id = ?",
			userId, friendId, friendId, userId).
		Order("created_at desc").Find(&list).Error
	return
}
